package com.example.taskdemo.services;


import android.util.Log;

import androidx.lifecycle.Observer;

import com.example.taskdemo.DataSingle;
import com.example.taskdemo.bean.DistrubtuionData;
import com.example.taskdemo.bean.PlayData;
import com.example.taskdemo.bean.PlayVideoData;
import com.example.taskdemo.hanlder.AIUILocalHandler;
import com.example.taskdemo.hanlder.MSlamwareAgent;
import com.example.taskdemo.tasks.BaseTask;
import com.example.taskdemo.tasks.DistrubutionTask;
import com.example.taskdemo.tasks.ListTask;
import com.example.taskdemo.tasks.PlayTask;
import com.example.taskdemo.tasks.PlayVideoTask;

import java.security.AlgorithmConstraints;
import java.util.List;

public class AIUIService extends BaseServiceListenner{
    private final String TAG = "AIUIService";
    private Observer<PlayData> playDataObserver;
    private Observer<BaseTask> baseTaskObserver;
    private Observer<ListTask> listTaskObserver;
    private Observer<DistrubtuionData> distrubtuionDataObserver;
    private Observer<PlayVideoData> playVideoDataObserver;
    @Override
    protected void initLiveDataObserver() {
        super.initLiveDataObserver();
        playDataObserver = new Observer<PlayData>() {
            @Override
            public void onChanged(PlayData playData) {
                if(playData ==null ||playData.getPlayState()==null || playData.getPlayState()== PlayData.PLAYState.NO){
                    return;
                }
                switch (playData.getPlayState()){
                    case NO:
                        break;
                    case INIT:
                        Log.d(TAG, "onChanged: 开始播放");
                        playdistributionTitle(playData.getText());
                        break;
                    case PLAYING:
                        Log.d(TAG, "onChanged: 播放中");
                        DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.RUN);
                        break;
                    case FINISH:
                        Log.d(TAG, "onChanged: 播放完成");
                        DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.FINISH);
                        break;
                    case ERROR:
                        break;
                }
                DataSingle.getInstance().UPPlayState(PlayData.PLAYState.NO);
            }
        };
        DataSingle.getInstance().getPlayDataMutableLiveData().observe(this,playDataObserver);
        baseTaskObserver = new Observer<BaseTask>() {
            @Override
            public void onChanged(BaseTask baseTask) {
                if(baseTask ==null ||baseTask.getTaskState() ==null || baseTask.getTaskState() == BaseTask.TASKSTATE.NO){
                    return;
                }
                switch (baseTask.getTaskState()){
                    case INIT:
                        if(baseTask instanceof PlayTask){
                            DataSingle.getInstance().getPlayDataMutableLiveData().postValue(((PlayTask) baseTask).getPlayData());
                        }
                        if(baseTask instanceof DistrubutionTask){
                            DataSingle.getInstance().getDistrubtuionDataMutableLiveData().postValue(((DistrubutionTask) baseTask).getDistrubtuionData());
                        }
                        if(baseTask instanceof PlayVideoTask){
                            DataSingle.getInstance().getPlayVideoDataMutableLiveData().postValue(((PlayVideoTask) baseTask).getPlayVideoData());
                        }
                        break;
                    case RUN:
                        Log.d(TAG, "onChanged: 任务执行中");
                        break;
                    case FINISH:
                        Log.d(TAG, "onChanged: 任务结束");
                        DataSingle.getInstance().UPListState(ListTask.LISTSTSK.NEXTTASK);
                        break;
                }
                DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.NO);
            }
        };
        DataSingle.getInstance().getBaseTaskMutableLiveData().observe(this,baseTaskObserver);
        listTaskObserver = new Observer<ListTask>() {
            @Override
            public void onChanged(ListTask listTask) {
                if(listTask ==null || listTask.getListstsk() ==null || listTask.getListstsk() == ListTask.LISTSTSK.NO)return;
                switch (listTask.getListstsk()){
                    case STARTASK:
                        DataSingle.getInstance().UPTask(listTask.getListTask().get(0));
                        break;
                    case NEXTTASK:
                        if(DataSingle.getInstance().getListTaskMutableLiveData().getValue().initNextData()){
                            Log.d(TAG, "onChanged: 开始执行下一个任务");
                            DataSingle.getInstance().UPTask(listTask.getListTask().get(0));
                        }else {
                            DataSingle.getInstance().UPListState(ListTask.LISTSTSK.FINISH);
                        }
                        break;
                    case FINISH:
                        Log.d(TAG, "onChanged: 完成所有任务");
                        break;
                }
            }
        };
        DataSingle.getInstance().getListTaskMutableLiveData().observe(this,listTaskObserver);
        distrubtuionDataObserver = new Observer<DistrubtuionData>() {
            @Override
            public void onChanged(DistrubtuionData distrubtuionData) {
                if(distrubtuionData ==null || distrubtuionData.getDistributionstate()==null || distrubtuionData.getDistributionstate() == DistrubtuionData.DISTRIBUTIONSTATE.NO)return;
                switch (distrubtuionData.getDistributionstate()) {
                    case INIT:
                        gotoPose(distrubtuionData.getText());
                        break;
                    case DISTRUBUTIONING:
                        Log.d(TAG, "onChanged: 配送中");
                        DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.RUN);
                        break;
                    case FINISH:
                        Log.d(TAG, "onChanged: 配送完成");
                        DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.FINISH);
                        break;
                }
            }
        };
        DataSingle.getInstance().getDistrubtuionDataMutableLiveData().observe(this,distrubtuionDataObserver);
        playVideoDataObserver = new Observer<PlayVideoData>() {
            @Override
            public void onChanged(PlayVideoData playVideoData) {
                if(playVideoData ==null || playVideoData.getPlayvideostate() ==null || playVideoData.getPlayvideostate() == PlayVideoData.PLAYVIDEOSTATE.NO)return;
                switch (playVideoData.getPlayvideostate()){
                    case INIT:
                        playVideo(playVideoData.getText());
                        break;
                    case PLAYING:
                        Log.d(TAG, "onChanged: 播放视频中");
                        DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.RUN);
                        break;
                    case FINISH:
                        Log.d(TAG, "onChanged: 视频播放完成");
                        DataSingle.getInstance().UPTaskState(BaseTask.TASKSTATE.FINISH);
                        break;
                }
            }
        };
        DataSingle.getInstance().getPlayVideoDataMutableLiveData().observe(this,playVideoDataObserver);
    }
    /**
     * 播放视频
     * @param string
     */
    private void playVideo(String string){
        AIUILocalHandler.getInstance().playVideo(string);
    }
    /**
     * 前往目的地
     * @param string
     */
    private void gotoPose(String string){
        MSlamwareAgent.getInstance().gotoPose(string);
    }
    /**
     * 播报内容
     */
    private void playdistributionTitle(String string) {
        AIUILocalHandler.getInstance().broadcast(string);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        DataSingle.getInstance().getPlayDataMutableLiveData().removeObservers(this);
    }
}
